var APP_ID = 'pBUe2GPkDpjHB3XGAhR3nysA-gzGzoHsz';
var APP_KEY = 'PaVsCucQl2xEzHOeNjBuYw1U';
$(document).ajaxSend(function(e, jqXHR, options) {
    var now = new Date();
    var millions = now.getTime();
    var hash = CryptoJS.MD5(millions + APP_KEY);
    jqXHR.setRequestHeader('X-LC-Id', APP_ID);
    jqXHR.setRequestHeader('X-LC-Sign', hash + ',' + millions)
});
var BaseModel = Backbone.Model.extend({
    idAttribute: 'objectId',
    objectClass: '',
    urlRoot: function() {
        return 'https://leancloud.cn/1.1/classes/' + this.objectClass
    },
    toJSON: function(options) {
        if (options && options.onlyChanged) {
        	console.log(this.changedAttributes(),options.onlyChanged)
           return  this.changedAttributes()
        } else {
            return _.clone(this.attributes);
        }
    }
});
var BaseCollection = Backbone.Collection.extend({
    url: function() {
        return 'https://leancloud.cn/1.1/classes/' + this.model.prototype.objectClass
    },
    parse: function(resp, options) {
    console.log(options)
        if (resp && resp.results) {
            return resp.results;
        }
        return resp
    }
})
var BaseCollectionView = Backbone.View.extend({
    subView: null,
    _initialize: function() {
        this.listenTo(this.collection, 'reset', this.render),
            this.listenTo(this.collection, 'add', this.addOne)
        this._views = [];
    },
    createSubView: function(model) {
        var ViewClass = this.subView || Backbone.View;
        var v = new ViewClass({ model: model });
        this._views.push(v);
        return v;
    },
    addOne: function(model) {
        //console.log(this.$el,this.createSubView(model).render().$el);
        this.$el.append(this.createSubView(model).render().$el)
    },
    render: function() {
        var _this = this;
        _.each(this._views, function(subView) {
            subView.remove().off()
        });
        this._views = [];
        //console.log(this.collection,this)
        if (!this.collection) return this;
        this.collection.each(function(model) {
            _this.addOne(model)
        });
    }
})


var BasePage = Backbone.View.extend({
    hide: function() {
        this.$el.hide();
    },
    show: function() {
        this.$el.show();
    }
})
